﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using MenusHunter.WinRT.Core.MVVM;
using MenusHunter.WinRT.Core.Models;
using MenusHunter.WinRT.Core.Services.Food;
using MenusHunter.WinRT.Core.Services.Restaurant;
using Windows.Devices.Geolocation;

namespace MenusHunter.WinRT.Core.ViewModels
{
    public class HomePageViewModel : ViewModelBase
    {
        private ObservableCollection<Restaurant> _restaurants;
        private ObservableCollection<Food> _foodsOfRestaurants;
        private ReplayCommand<Restaurant> _getFoodsForRestaurantCommand;
        private ReplayCommand _getListRestaurantsCommand;
        private Restaurant _selectedRestaurant;
        private ReplayCommand _viewAsMapCommand;

        #region Properties

        public ObservableCollection<Restaurant> Restaurants
        {
            get { return _restaurants; }
            set { SetProperty(ref _restaurants, value); }
        }

        public ObservableCollection<Food> FoodsOfRestaurant
        {
            get { return _foodsOfRestaurants; }
            set { SetProperty(ref _foodsOfRestaurants, value); }
        }

        public Restaurant SelectedRestaurant
        {
            get { return _selectedRestaurant; }
            set { SetProperty(ref _selectedRestaurant, value); }
        }

        #endregion

        #region Commands

        public ReplayCommand<Restaurant> GetFoodsForRestaurantCommand
        {
            get
            {
                if (_getFoodsForRestaurantCommand == null)
                {
                    _getFoodsForRestaurantCommand = new ReplayCommand<Restaurant>(async param =>await GetFoodsForRestaurant(),
                                                                                  param => Idle);
                }
                return _getFoodsForRestaurantCommand;
            }
        }

        public ReplayCommand GetListRestaurantsCommand
        {
            get
            {
                if (_getListRestaurantsCommand == null)
                {
                    _getListRestaurantsCommand = new ReplayCommand(param => GetListRestaurants(), param => Idle);
                }
                return _getListRestaurantsCommand;
            }
            set { SetProperty(ref _getListRestaurantsCommand, value); }
        }

        #endregion

        public async Task GetFoodsForRestaurant()
        {
            if (SelectedRestaurant != null)
            {
                IsBusy = true;
                var result = await FoodsManager.GetListFoodRestaurant(SelectedRestaurant.Id);
                IsBusy = false;
                FoodsOfRestaurant = result;
               
            }
        }

        public async void GetListRestaurants()
        {
            IsBusy = true;
            var result = await RestaurantsManager.GetListRestaurantAsync();
            Restaurants = result;
             IsBusy = false;
        }

        public async Task GetListRestaurantsAround(int offset, int limit, int radius = 2)

        {
            IsBusy = true;
            var locator = new Geolocator();
            var location = await locator.GetGeopositionAsync();
            var currenLat = location.Coordinate.Latitude;
            var currentLong = location.Coordinate.Longitude;
            var result =
                await RestaurantsManager.GetListRestaurantAroundAsync(currenLat, currentLong, radius, offset, limit);
            Restaurants = result;
            IsBusy = false;

        }
    }
}